<?php


namespace Home\Controller;

class MyController extends HomeController {
    //直播认证
    public function suthentic(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }
            $key = $input['key'];
            $uid = $input['uid'];
            $agree = $input['agree'];
            $realname = $input['realname'];
            $mobile = $input['mobile'];
            $rz_province = $input['rz_province'];
            $rz_city = $input['rz_city'];
            $rz_area = $input['rz_area'];
            $rz_detail = $input['rz_detail'];
            $qqnum = $input['qqnum'];
            $email = $input['email'];
            $idnum = $input['idnum'];
            $sys = $input['sys'];
            $again_identity = $input['again_identity'];//重新认证提交的身份证id
            $again_cover = $input['again_cover'];//重新认证提交的直播封面图id

            if($sys == 'IOS'){
                $base_identity_img = $input['base_identity_img'];//身份证
                $base_zb_cover = $input['base_zb_cover'];//主播封面
                if(empty($base_identity_img) && empty($again_identity)) return api_return(2,'请上传身份证图片');
                if(empty($base_zb_cover) && empty($again_cover)) return api_return(2,'请上传主播封面');

                $skill_id = M('skill')->where(['name' => $input['skill_id']])->getField('id');
            }else{
                $skill_id = $input['skill_id'];
            }

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');
            if(empty($agree)) return api_return(2,'请同意协议');
            if(empty($realname)) return api_return(2,'请填写真实姓名');
            if(empty($mobile)) return api_return(2,'请填写手机号');

            $pattern = "/^1[34578]\d{9}$/";
            $istrue=preg_match($pattern, $mobile);
            if($istrue!=1) return api_return(2,"手机号格式不正确");

            if(!$rz_province || !$rz_city || !$rz_area) return api_return(2,'请选择地址');
            if(!$rz_detail) return api_return(2,'请填写详细地址');
            if(!$qqnum) return api_return(2,'请填写QQ号');
            if(!$email) return api_return(2,'请填写邮箱');

            $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
            $ispass=preg_match($pattern, $email);
            if($ispass!=1) return api_return(2,"邮箱格式不正确");
            if(!$idnum) return api_return(2,'请填写身份证号');
            $pattern = "/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/";
            $ispass=preg_match($pattern, $idnum);
            if($ispass!=1) return api_return(2,"身份证格式不正确");
            if(!$skill_id) return api_return(2,'请选择才艺');

            //身份证照片
            if($base_identity_img || $again_identity){
                //IOS图片上传
                if($base_identity_img){
                    $identity_img_id = base64_one_img($base_identity_img);//返回图片id
                }else{
                    $identity_img_id = $again_identity;
                }
            }else{
                //安卓图片上传
                $cover_file = $_FILES['identity_img'];
                if($cover_file['name'] == ''){
                    $cover_id = $again_cover;
                }else{
                    if (!is_dir("Uploads/Picture/".date("Y-m-d"))) {
                        mkdir("Uploads/Picture/".date("Y-m-d"));
                    }

                    if (is_uploaded_file($cover_file['tmp_name'])) {
                        $str = stristr($cover_file['name'], '.');
                        $cover_path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . "identity".$uid. $str;

                        if(copy($cover_file['tmp_name'], $cover_path)){
                            $cover['path'] = "/".$cover_path;
                            $cover['status'] = 1;
                            $cover['create_time'] = time();

                            $res_identity_img_id = M('picture')->add($cover);

                            if($res_identity_img_id){
                                $identity_img_id = $res_identity_img_id;
                            }else{
                                return api_return(2,'身份证图片上传失败');
                            }
                        }else {
                            return api_return(2,'身份证图片上传失败2');
                        }
                    }
                }
            }

            //直播封面图
            if($base_zb_cover || $again_cover){
                //IOS图片上传
                if($base_zb_cover){
                    $cover_id = base64_one_img($base_zb_cover);//返回图片id
                }else{
                    $cover_id = $again_cover;
                }
            }else{
                //安卓图片上传
                $zb_cover_file = $_FILES['zb_cover'];

                if($zb_cover_file['name'] == ''){
                    $cover_id = $again_cover;
                }else{
                    if (!is_dir("Uploads/Picture/".date("Y-m-d"))) {
                        mkdir("Uploads/Picture/".date("Y-m-d"));
                    }

                    if (is_uploaded_file($zb_cover_file['tmp_name'])) {
                        $str = stristr($zb_cover_file['name'], '.');
                        $cover_path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . "zb_cover".$uid. $str;

                        if(copy($zb_cover_file['tmp_name'], $cover_path)){
                            $cover['path'] = "/".$cover_path;
                            $cover['status'] = 1;
                            $cover['create_time'] = time();

                            $result_cover_id = M('picture')->add($cover);

                            if($result_cover_id){
                                $cover_id = $result_cover_id;
                            }else{
                                return api_return(2,'主播封面图片上传失败');
                            }
                        }else {
                            return api_return(2,'主播封面图片上传失败');
                        }
                    }
                }
            }

            $where['id']=$uid;
            $data=array(
                'realname'=>$realname,
                'rz_mobile'=>$mobile,
                'rz_province'=>$rz_province,
                'rz_city'=>$rz_city,
                'rz_area'=>$rz_area,
                'rz_detail'=>$rz_detail,
                'qqnum'=>$qqnum,
                'email'=>$email,
                'idnum'=>$idnum,
                'skill_id'=>$skill_id,
                'identity_img'=>$identity_img_id,
                'rz_status'=>1,
                'cover' => $cover_id
            );

            $res=M('user')->where($where)->save($data);
            if($res){
                return api_return(1,'认证提交成功');
            }else{
                return api_return(2,'认证提交失败');
            }
        }
    }

    /**
     * 直播认证回显
     * */
    public function suthentic_info(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'请传入用户id');

            $field = 'realname,rz_mobile,rz_province,rz_area,rz_city,rz_detail,qqnum,email,idnum,skill_id,identity_img,cover,rz_status';
            $info = M('user')->field($field)->where(['id' => $uid])->find();

            $info['identity_img_id'] = $info['identity_img'];
            $info['cover_id'] = $info['cover'];
            $info['province'] = getarea($info['rz_province']);
            $info['area'] = getarea($info['rz_area']);
            $info['city'] = getarea($info['rz_city']);
            $info['identity_img'] = get_img($info['identity_img']);
            $info['mobile'] = $info['rz_mobile'];
            $info['cover'] = get_img($info['cover']);
            $info['skill'] = M('skill')->where(['id' => $info['skill_id']])->getField('name');

            if($info){
                return api_return(1,'获取数据成功',$info);
            }else{
                return api_return(2,'获取数据失败');
            }
        }
    }


    //编辑资料
    public  function edit_myinfo(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $nickname = $input['nickname'];
            $sex = $input['sex'];
            $job = $input['job'];
            $age = $input['age'];
            $province = $input['province'];
            $city = $input['city'];
            $area = $input['area'];
            $detail = $input['detail'];
            $sign = $input['sign'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');
            if(empty($nickname)) return api_return(2,'昵称不能为空');
            if(empty($sex)) return api_return(2,'性别不能为空');
            if(empty($job)) return api_return(2,'请输入职业');
            if(empty($age)) return api_return(2,'请输入年龄');
            if(!$province && !$city && !$area) return api_return(2,'请选择地址');
            if(empty($detail)) return api_return(2,'请填写详细地址');

            //头像上传
            $base_avatar = $input['base_avatar'];//IOS参数
            $old_avatar = $input['old_avatar'];//安卓参数
            $avatar_file = $_FILES['avatar'];//安卓参数
            if(!(($old_avatar || $avatar_file['name'] != '') || $base_avatar)) return api_return(2,'请上传头像');

            if($base_avatar){
                if(strpos($base_avatar,'/Uploads/Picture/') !== false){
                    //IOS未修改封面图
                    $base_avatar = str_replace("http://".$_SERVER['HTTP_HOST'],'',$base_avatar);
                    $avatar_id = M("picture")->where(['path' => $base_avatar])->getField('id');
                }else{
                    //IOS修改封面图
                    $avatar_id = base64_one_img($base_avatar);
                }
            }else{

                if($old_avatar){
                    $old_avatar = str_replace("http://".$_SERVER['HTTP_HOST'],'',$old_avatar);
                    $avatar_id = M("picture")->where(['path' => $old_avatar])->getField('id');
                }else{
                    if (!is_dir("Uploads/Picture/".date("Y-m-d"))) {
                        mkdir("Uploads/Picture/".date("Y-m-d"));
                    }
                    if (is_uploaded_file($avatar_file['tmp_name'])) {
                        $str = stristr($avatar_file['name'], '.');
                        $avatar_path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . "cover" . $uid . $str;

                        if(copy($avatar_file['tmp_name'], $avatar_path)){
                            $avatar['path'] = "/".$avatar_path;
                            $avatar['status'] = 1;
                            $avatar['create_time'] = time();

                            $result_avatar_id = M('picture')->add($avatar);

                            if($result_avatar_id){
                                $avatar_id = $result_avatar_id;
                            }else{
                                return api_return(2,'直播封面图片上传失败');
                            }
                        }else {
                            return api_return(2,'直播封面图片上传失败');
                        }
                    }
                }
            }

            //直播封面图
            $is_anchor = M('user')->where(['id' => $uid,'is_anchor' => 1, 'rz_status' => 3])->count();//主播，非主播
            if($is_anchor > 0){
                $base_cover = $input['base_cover'];//IOS参数
                $old_cover = $input['old_cover'];//安卓参数
                $cover_file = $_FILES['cover'];//安卓参数
                if(!(($old_cover || $cover_file['name'] != '') || $base_cover)) return api_return(2,'请上传直播封面图片');

                if($base_cover){
                    if(strpos($base_cover,'/Uploads/Picture/') !== false){
                        //IOS未修改封面图
                        $base_cover = str_replace("http://".$_SERVER['HTTP_HOST'],'',$base_cover);
                        $cover_id = M("picture")->where(['path' => $base_cover])->getField('id');
                    }else{
                        //IOS修改封面图
                        $cover_id = base64_one_img($base_cover);
                    }
                }else{

                    if($old_cover){
                        $old_cover = str_replace("http://".$_SERVER['HTTP_HOST'],'',$old_cover);
                        $cover_id = M("picture")->where(['path' => $old_cover])->getField('id');
                    }else{
                        if (!is_dir("Uploads/Picture/".date("Y-m-d"))) {
                            mkdir("Uploads/Picture/".date("Y-m-d"));
                        }
                        if (is_uploaded_file($cover_file['tmp_name'])) {
                            $str = stristr($cover_file['name'], '.');
                            $cover_path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . "cover" . $uid . $str;

                            if(copy($cover_file['tmp_name'], $cover_path)){
                                $cover['path'] = "/".$cover_path;
                                $cover['status'] = 1;
                                $cover['create_time'] = time();

                                $result_cover_id = M('picture')->add($cover);

                                if($result_cover_id){
                                    $cover_id = $result_cover_id;
                                }else{
                                    return api_return(2,'直播封面图片上传失败');
                                }
                            }else {
                                return api_return(2,'直播封面图片上传失败');
                            }
                        }
                    }
                }
            }

            $where['id']=$uid;
            $data=array(
                'nickname'=>$nickname,
                'age' => $age,
                'sex'=>$sex,
                'province'=>$province,
                'city'=>$city,
                'area'=>$area,
                'detail'=>$detail,
                'sign'=>$sign,
                'job'=>$job,
                'cover'=>isset($cover_id)?$cover_id:0,
                'edit_img' => isset($avatar_id)?$avatar_id:0
            );

            $res=M('user')->where($where)->save($data);
            if($res){
                return api_return(1,'编辑成功');
            }else{
                return api_return(2,'编辑失败');
            }
        }
    }

    /**
     * 我的粉丝
     * */
    public function my_follower(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $cuid = $input['cuid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($cuid)) return api_return(2,'主播id不能为空');

            $offset = ($n-1)*$num;
            $list = M('concern')->field('uid')->where(['cuid' => $cuid,'status' => 1])->order('create_time desc')->limit($offset,$num)->select();
            $userinfo = [];
            foreach($list as $k => $v){
                $info = M('user')->field('id,nickname,exp')->where(['id' => $v['uid']])->find();
                $userinfo[$k]['avatar'] = get_avatar($info['id']);
                $userinfo[$k]['nickname'] = $info['nickname']?$info['nickname']:'';
                $userinfo[$k]['rank'] = exp_rank($info['exp']);
            }

            if($userinfo){
                return api_return(1,'获取数据成功',$userinfo);
            }else{
                return api_return(1,'暂无更多数据',[]);
            }
        }
    }

    /**
     * 新增关注消息
     * */
    public function follower_mess(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $cuid = $input['cuid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($cuid)) return api_return(2,'主播id不能为空');

            $offset = ($n-1)*$num;
            $list = M('concern')->field('uid,create_time')->where(['cuid' => $cuid,'status' => 1])->order('create_time desc')->limit($offset,$num)->select();
            $userinfo = [];
            foreach($list as $k => $v){
                $nickname = M('user')->where(['id' => $v['uid']])->getField('nickname');
                $userinfo[$k]['msee'] = $nickname.'关注了您';
                $userinfo[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }

            if($userinfo){
                return api_return(1,'获取数据成功',$userinfo);
            }else{
                return api_return(1,'暂无更多数据',[]);
            }
        }
    }

    //我的资料
    public  function myinfo(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $where['id'] = $uid;
            $res=M('user')->field('number,nickname,sex,age,province,city,area,detail,avatar_img,job,sign,cover')->where($where)->find();

            if($res){
                $province = getarea($res['province']);
                $city = getarea($res['city']);
                $area = getarea($res['area']);

                $res['address'] = $province.$city.$area;

                $res['avatar_img']=get_avatar($uid);
                $res['cover']=getImgurl($res['cover'])?getImgurl($res['cover']):'';
                $res['job_id'] = $res['job'];
                $job_name = M('job')->where(['id' => $res['job']])->getField('name');
                $res['job_name'] = $job_name?$job_name:'';
                return api_return(1,'success',$res);
            }else{
                return api_return(1,'无数据',array());
            }
        }
    }

      //上传头像
    public function uploadAvatar(){
        if(IS_POST){
            $json_data = php_input();
            if($json_data){
                $input = json_decode($json_data,true);
            }else{
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $base_avatar = $input['base_avatar'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $cover_file = $_FILES['avatar'];
            if(empty($base_avatar) && $cover_file == '') return api_return(2,'请上传头像');

            if($base_avatar){
                $cover_id = base64_one_img($base_avatar);
            }else{
                if (!is_dir("Uploads/Picture/".date("Y-m-d"))) {
                    mkdir("Uploads/Picture/".date("Y-m-d"));
                }

                if (is_uploaded_file($cover_file['tmp_name'])) {
                    $str = stristr($cover_file['name'], '.');
                    $cover_path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . $str;

                    if(copy($cover_file['tmp_name'], $cover_path)){
                        $cover['path'] = "/".$cover_path;
                        $cover['status'] = 1;
                        $cover['create_time'] = time();
                        $result_cover_id = M('picture')->add($cover);
                        if($result_cover_id){
                            $cover_id = $result_cover_id;
                        }else{
                            return api_return(2,'头像上传失败');
                        }
                    }else {
                        return api_return(2,'头像上传失败');
                    }
                }
            }

            $data['edit_img'] = $cover_id;
            $res=M('user')->where(['id' => $uid])->save($data);
//             echo M('user')->getLastSql();
            // exit;
            if($res){
                return api_return(1,'上传成功');
            }else{
                return api_return(2,'上传失败');
            }
        }
    }

    //地址列表
    public function address_list(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $str=($n-1)*$num;
            $where['uid']=$uid;
            $where['status']=array('neq',2);
            $res=M('address')->field('id,username,mobile,province,city,area,detail,is_default')->where($where)->order('id desc')->limit($str,$num)->select();
            if($res){
                foreach ($res as $k => $v) {
                    $province=getarea($v['province']);
                    $city=getarea($v['city']);
                    $area=getarea($v['area']);
                    $res[$k]['address']=$province.$city.$area.$v['detail'];
                    unset($res[$k]['province']);
                    unset($res[$k]['city']);
                    unset($res[$k]['area']);
                    unset($res[$k]['detail']);
                }

                return api_return(1,'success',$res);
            }else{
                return api_return(1,'无数据',array());
            }
        }
    }

    //添加地址
    public function addandedit_address(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $username = $input['username'];
            $mobile = $input['mobile'];
            $province = $input['province'];
            $city = $input['city'];
            $area = $input['area'];
            $detail = $input['detail'];
            $is_default = $input['is_default'];
            $id = $input['id'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');
            if(empty($username)) return api_return(2,'姓名不能为空');
            if(empty($mobile)) return api_return(2,'手机号不能为空');

            $pattern = "/^1[34578]\d{9}$/";
            $istrue=preg_match($pattern, $mobile);
            if($istrue!=1) return api_return(2,"手机号格式不正确");

            if(!$province || !$city ||!$area) return api_return('请选择地址');
            if(empty($detail)) return api_return(2,'请填写详细地址');

            if($is_default == 1){
                 $map['uid'] = $uid;
                 $map['is_default'] = 1;
                 $def=M('Address')->where($map)->find();
                 if($def){
                    $where['id']=$def['id'];
                    $arr['is_default']='0';
                    M('Address')->where($where)->save($arr);
                    $data['is_default']='1';
                 }
            }

            $data=array(
                'uid'=>$uid,
                'username'=>$username,
                'mobile'=>$mobile,
                'province'=>$province,
                'city'=>$city,
                'area'=>$area,
                'detail'=>$detail,
                'is_default'=>$is_default
            );

            if($id){
                $where['id'] = $id;
                $res=M('address')->where($where)->save($data);
                if($res){
                    return api_return(1,'编辑成功');
                }else{
                    return api_return(2,'编辑失败');
                }
            }else{
                $data['create_time']=time();
                $res=M('address')->add($data);
                 if($res){
                    return api_return(1,'新增成功');
                }else{
                    return api_return(2,'新增失败');
                }
            }
        }
    }

    /**
     * 编辑收货地址（回显）
     * */
    public function address_detail(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $id = $input['id'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($id)) return api_return(2,'收货地址id不能为空');

            $info = M('address')->field('id,username,mobile,province province_id,city city_id,area area_id,detail,is_default')->where(['id' => $id])->find();

            if($info){
                $info['province'] = getarea($info['province_id']);
                $info['city'] = getarea($info['city_id']);
                $info['area'] = getarea($info['area_id']);

                return api_return(1,'获取数据成功',$info);
            }else{
                return api_return(1,'获取数据失败',[]);
            }
        }
    }

    //删除地址
    public function delAddress(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $id = $input['id'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($id)) return api_return(2,'id不能为空');

            $where['id']=$id;
            $arr['status']=2;
            $del=M('Address')->where($where)->save($arr);
            if($del){
                return api_return(1,'删除成功');
            }else{
                return api_return(2,'删除失败');
            }
        }

    }

    /**
     * 设置默认地址
     * a_id   地址id
     * uid    用户id
     */
    public function setDefaultAddress(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $a_id = $input['a_id'];
            $uid = $input['uid'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($a_id)) return api_return(2,'地址id不能为空');
            if(empty($uid)) return api_return(2,'uid不能为空');

            $map['id']=$a_id;
            $map['uid']=$uid;
            $addr=M('Address')->where($map)->find();

            if(!$addr){
                return api_return('2','该地址不存在');
            }
            if($addr['is_default']==1){
                return api_return('2','该地址已经是默认地址');
            }else{
                $data['is_default']=1;
                $data['uid']=$uid;
                $addr=M('Address')->field('id,uid,is_default')->where($data)->find();
                if(!$addr){
                    //如果之前没有默认地址，则直接设置
                    $data['is_default']=1;
                    $res=M('Address')->where($map)->save($data);
                    if($res){
                        return api_return(1,'设置成功');
                    }else{
                        return api_return(2,'设置失败');
                    }
                }else{
                    //如果之前有默认地址
                    $data['is_default']='0';
                    $data['id']=$addr['id'];
                    $res=M('Address')->where('id='.$addr['id'])->save($data);       //取消默认地址
                    if($res){
                        //设置新的默认地址
                        $arr['is_default']=1;
                        $res=M('Address')->where($map)->save($arr);
                        if($res){
                            return api_return(1,'设置成功');
                        }else{
                            return api_return(2,'设置失败');
                        }
                    }
                }
            }
        }
    }

    //动态列表
    public function trends_list(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if ($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $str=($n-1)*$num;

            $where['uid']=$uid;
            $res=M('trends')->where($where)->limit($str,$num)->select();
            foreach($res as $k=>$v){
                $imgids=$v['imgs'];
                $imgs=explode(',', $imgids);
                $imgurl=array();
                for($i=0;$i<count($imgs);$i++){
                    $imgurl[$i]['img']=getImgurl($imgs[$i]);
                }
                $res[$k]['imgs']=$imgurl;
                $res[$k]['create_time'] = format_time($v['create_time']);
                unset($res[$k]['uid']);
            }

            if($res){
                return api_return(1,'获取数据成功',$res);
            }else{
                return api_return(1,'暂无更多数据',[]);
            }
        }
    }

    //发布动态
    public function fb_trends(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $content = $input['content'];

            if ($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');
            if(empty($content)) return api_return(2,'发布内容不能为空');

            $base_imgs = $input['base_imgs'];
            if($base_imgs){
                //IOS图片上传
                $imgs = base64_all_img($base_imgs);
                $imgs = substr($imgs,0,-1);
            }else{
                //上传多图
                $file = $_FILES['imgs'];
                if (!is_dir("Uploads/Picture/" . date("Y-m-d"))) {
                    mkdir("Uploads/Picture/" . date("Y-m-d"));
                }

                // $img_id = "";
                $imgid = array();
                for ($i = 0; $i < count($file['tmp_name']); $i++) {
                    $str = stristr($file['name'][$i], '.');
                    if ($str != '') {
//                        $path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . $i . "trends" . $uid . $str;
                        $path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . $str;
                    } else {
                        $path = '';
                    }

                    if ($path != '') {
                        if (copy($file['tmp_name'][$i], $path)) {
                            $imgurl['path'] = "/" . $path;
                            $imgurl['status'] = 1;
                            $imgurl['create_time'] = time();
                            $picture_id = D('picture')->add($imgurl);
                            if ($picture_id) {
                                $imgid[] = $picture_id;
                            }
                        } else {
                            return api_return(2,'提交失败');
                        }
                    }
                }
                $imgs = implode(',', $imgid);
            }

            $data['uid']=$uid;
            $data['imgs']=$imgs;
            $data['content']=$content;
            $data['create_time']=time();
            $res=M('trends')->add($data);
            if($res){
                return api_return(1,'发布成功');
            }else{
                return api_return(2,'发布失败');
            }
        }
    }

    //系统消息
    public function message(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $str=($n-1)*$num;

            $list=M('message')->field('id,title,content,create_time')->where(" (uid = ". $uid ." or uid = 0) and type = 1 ")->limit($str,$num)->select();

            foreach($list as $k=>$v){
                $data = [
                    'uid' => $uid,
                    'mid' => $v['id']
                ];
                M('official')->add($data);//系统消息已读
                $content=str_replace("/Uploads/Editor/","http://".$_SERVER['HTTP_HOST'].'/Uploads/Editor/', $v['content']);
                $list[$k]['content']=str_replace("/Public/static/kindeditor/","http://".$_SERVER['HTTP_HOST'].'/Public/static/kindeditor/', $content);
            }

            if($list){
                return api_return(1,'success',$list);
            }else{
                return api_return(1,'无数据',array());
            }
        }

    }

    //系统消息详情
    public function message_detail(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $id = $input['id'];
            $uid = $input['uid'];

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($id)) return api_return(2,'id不能为空');
            if(empty($uid)) return api_return(2,'uid不能为空');

            M('official')->add(['uid' => $uid, 'mid' => $id]);

            $list=M('message')->field('title,content,create_time')->where("id=$id")->find();
            $content=str_replace("/Uploads/Editor/","http://".$_SERVER['HTTP_HOST'].'/Uploads/Editor/', $list['content']);
            $list['content']=str_replace("/Public/static/kindeditor/","http://".$_SERVER['HTTP_HOST'].'/Public/static/kindeditor/', $content);
            if($list){
                return api_return(1,'success',$list);
            }else{
                return api_return(2,'获取数据失败');
            }
        }
    }

    //漂流瓶
    public function giftbag(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $userinfo=M('user')->field('level')->where("id=$uid")->find();
            $user_level=$userinfo['level']; //用户等级

            $user_id=M('user')->field('id')->where("level=$user_level")->select();
            $uids=array();
            foreach($user_id as $k=>$v){
                array_push($uids,$v['id']);
            }

            $isset= in_array($uid,$uids);
            if(!$isset){
                return api_return(1,'无数据',array());
            }else{
                $str=($n-1)*$num;
                $list= M('giftbag')->field('id,price,zs_num,day,level_id,user_level,type,create_time')
                    ->where('(user_level = 0 || user_level ='.$user_level.') and status = 1')
                    ->limit($str,$num)->select();
            }
            if($list){
                return api_return(1,'success',$list);
            }else{
                return api_return(1,'无数据',array());
            }
        }
    }

    //任务列表
    public function  mytask(){
        if(IS_POST){
            $json_data = php_input();

            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                $input = I('post.');
            }

            $key = $input['key'];
            $uid = $input['uid'];
            $n = $input['n']?$input['n']:1;
            $num = $input['num']?$input['num']:10;

            if($key != C('ONLY_KEY')) return api_return(2,'唯一标识错误');
            if(empty($uid)) return api_return(2,'用户id不能为空');

            $str=($n-1)*$num;
            $list= M('task')->limit($str,$num)->select();
            foreach($list as $k => $v){
                $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }

            if($list){
                return api_return(1,'success',$list);
            }else{
                return api_return(1,'无数据',array());
            }
        }
    }

}
